<template>
    <el-container>
        <el-container class="is-vertical">
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-input v-model="search.code" placeholder="订单编号" clearable  class="w165"></el-input>
                    <el-input v-model="search.userName" placeholder="学员" clearable  class="w165"></el-input>
                    <el-input v-model="search.mobile" placeholder="手机号" clearable  class="w165"></el-input>
                    <el-select v-model="search.isDistribution" placeholder="排课状态" clearable>
                        <el-option label="已排课" :value="1"></el-option>
                        <el-option label="待排课" :value="0"></el-option>
                    </el-select>
                    <el-date-picker v-model="search.timeRange" type="datetimerange" value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD HH:mm:ss"
							range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间" class="sDateSelect" style="margin-left:10px;"
						>
                    </el-date-picker>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-download" @click="exportHander">导出</el-button>
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :params="search" :apiObj="$API.transaction.list" remoteSort @dataChange="dataChange" :exportData="exportData" :formatExportData="formatExportData">
                    <el-table-column label="订单编号" prop="code" width="200" align="left">
                    </el-table-column>
                    <el-table-column label="学员" prop="user" align="center">
                        <template #default="scope">
                            {{scope.row.fromUser ? scope.row.fromUser.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="手机号" prop="user" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.fromUser ? scope.row.fromUser.mobile : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="年级" prop="user"  align="center" width="100">
                        <template #default="scope">
                            {{scope.row.param ? scope.row.param.gradeName : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="目前英语分数" prop="user" align="center" width="120">
                        <template #default="scope">
                            {{scope.row.param ? scope.row.param.englishScore : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="期望上课时间" prop="trialDate" align="center" width="120">
                        <template #default="scope">
                            {{scope.row.param ? scope.row.param.trialDate : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="排课状态" align="center" width="80">
                        <template #default="scope">
                            <el-tag size="mini" v-if="scope.row?.transactionTeacher?.status.value == 1" type="success">{{scope.row?.transactionTeacher?.status?.name}}</el-tag>
                            <el-tag size="mini" v-else type="danger">{{scope.row?.transactionTeacher?.status?.name}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="分配教练" align="center" width="140">
                        <template #default="scope">
                            {{scope.row?.transactionTeacher?.teacher?.name || '-'}}
                        </template>
                    </el-table-column>
                    <el-table-column label="金额" prop="amount"  align="center" width="100"></el-table-column>
                    <el-table-column label="交易时间" prop="createdAt" align="center" width="180"></el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="220">
                        <template #default="scope">
                            <el-button type="text" size="small" @click="table_appointment(scope.row)" v-if="permission.teacher && scope.row?.transactionTeacher?.status.value != 1">排课</el-button>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>

    <appointmentDialog v-if="dialog.appointment" ref="appointmentDialog" @success="handleSuccess"></appointmentDialog>

</template>

<script>
import { rolePermission } from '@/utils/permission'
import appointmentDialog from '../appointment/components/transactionTeacher.vue'
export default {
    components: {
        appointmentDialog
    },
    data() {
        return {
            dialog: {
                appointment: false,
            },
            search: {
                name: null,
                productType:[1],
                status:30
            },
            permission: {
                teacher: rolePermission('record-transaction-teacher')
			},
            exportData: {
                header: ['订单编号', '学员', '手机号', '年级', '目前英语分数', '期望上课时间', '排课状态', '分配教练', '金额', '交易时间'],
                column: ['code', 'userName', 'mobile', 'gradeName', 'englishScore', 'trialDate', 'distributionStatus', 'distributionTeacher', 'amount', 'createdAt'],
                filename: '体验课订单列表',
                formatData: true
            },
        }
    },
    mounted() {
        
    },
    methods: {
        dataChange(res, tableData) {
			let total = res.data.total
            let perPage = res.data.perPage
            let page = (this.$refs.table && this.$refs.table.currentPage) || 1
            tableData.forEach((item, index) => {
				item.index = page * perPage + index - perPage + 1
            })
        },
        // 预约
        table_appointment(row) {
            this.dialog.appointment = true
			this.$nextTick(() => {
				this.$refs.appointmentDialog.open('add', row)
			})
        },
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        exportHander() {
			this.$refs.table.exportHander(this.search)
		},
		formatExportData(item) {
            item.userName = item?.fromUser?.name || ''
            item.mobile = item?.fromUser?.mobile || ''
            item.gradeName = item?.param?.gradeName || ''
            item.englishScore = item?.param?.englishScore || ''
            item.trialDate = item?.param?.trialDate || ''
            item.distributionStatus = item?.transactionTeacher?.status?.name || ''
            item.distributionTeacher = item?.transactionTeacher?.teacher?.name || ''
            return item
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        }
    }
}
</script>

<style>
.position {
    display: flex;
    align-items: center;
}
.position-item {
    margin-right: 5px;
}
.position-item:last-child {
    margin-right: 0;
}
</style>
